package com.cn.ccp.service.classschedule;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.RowMapper;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleDetailBean;
import com.cn.ccp.service.BasicService;
import com.cn.ccp.util.Contants;
import com.cn.ccp.util.MsgConstants;

/**
 * 课程表service
 * @author Administrator
 *
 */
public class ClassScheduleDetailService extends BasicService{
	private Log log = LogFactory.getLog(ClassScheduleDetailService.class);
		
	
	/**
	 * 添加课程表
	 * @param bo
	 * @return
	 */
	public boolean save(ClassScheduleDetailBean bo){
		String sql = "insert into class_schedule_detail(class_schedule_id,week,class_schedule_setting_id,subjects,stat_week,end_week,teacher_id,class_address,week_type) values (?,?,?,?,?,?,?,?,?)";
		return  jdbcTemplate.update(sql ,new Object[]{bo.getClassScheduleId(),bo.getWeek(),bo.getScheduleSettingId(),bo.getSubjects(),bo.getStartWeek(),bo.getEndWeek(),bo.getTeacherId(),bo.getClassAddress(),bo.getWeekType()}) > 0;
	}
	
	/**
	 * 更新课程表内容 
	 * @param bo
	 * @return
	 */
	public boolean update(ClassScheduleDetailBean bo){
		StringBuffer buf = new StringBuffer();
		buf.append("update class_schedule_detail  ");
		buf.append("set subjects = ?,");
		buf.append(" stat_week = ?,");
		buf.append(" end_week = ?,");
		buf.append(" teacher_id = ?,");
		buf.append(" class_address = ?,");
		buf.append(" week_type = ? ");
		buf.append("where id = ?");
		return jdbcTemplate.update(buf.toString() ,new Object[]{bo.getSubjects(),bo.getStartWeek(),bo.getEndWeek(),bo.getTeacherId(),bo.getClassAddress(),bo.getWeekType(),bo.getId()}) > 0; 
	}
	
	
	public boolean delete(int id) {
		String sql = "delete from class_schedule_detail where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{id}) > 0;
	}
	
	/**
	 *根据id查询学期设置
	 * @param id
	 * @return
	 */
	public Object findClassScheduleById(int id) {
		String sql = "SELECT * FROM class_schedule_detail WHERE id = ?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id }, new ClassScheduleDetailSimpleMapper());
	}
	
	public List pageQuery(String strScheduleId) {
		String sql = "select detail.*,setting.* from class_schedule_detail detail,class_schedule_settings setting where detail.class_schedule_id = ?  and detail.class_schedule_setting_id = setting.id  order by class_schedule_setting_id ,week";
		return jdbcTemplate.query(sql, new Object[] { strScheduleId },new ClassScheduleDetailMapper());
	}
			
	public List pageQueryForUserByGradeId(String gradedId,int strTermSettingId) {
		String sql = "select detail.*,setting.* from class_schedule_detail detail,class_schedule_settings setting where detail.class_schedule_id = (select id from class_schedule  where  find_in_set(?, grade_id) and term_setting_id = ? )  " +
				    "and detail.class_schedule_setting_id = setting.id  order by detail.class_schedule_setting_id ,detail.week";
		return jdbcTemplate.query(sql, new Object[] { gradedId, strTermSettingId},new ClassScheduleDetailMapper());
	}
	
	public List pageQueryForTeacher(String teacerId,int strTermSettingId) {
		String sql = "select detail.*,setting.* from class_schedule_detail detail,class_schedule_settings setting where detail.class_schedule_id = (select id from class_schedule where term_setting_id = ? and teacher_id = ? )  and detail.class_schedule_setting_id = setting.id  order by class_schedule_setting_id ,week";
		return jdbcTemplate.query(sql, new Object[] { strTermSettingId,teacerId },new ClassScheduleDetailMapper());
	}
	
	
	
	 class ClassScheduleDetailSimpleMapper implements RowMapper {
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				ClassScheduleDetailBean bean = new ClassScheduleDetailBean();
				bean.setId(rs.getInt("id"));
				bean.setClassScheduleId(rs.getInt("class_schedule_id"));
				bean.setWeek(rs.getInt("week"));
				bean.setScheduleSettingId(rs.getInt("class_schedule_setting_id"));
				bean.setSubjects(rs.getString("subjects"));
				bean.setStartWeek(rs.getString("stat_week"));
				bean.setEndWeek(rs.getString("end_week"));
				bean.setTeacherId(rs.getString("teacher_id"));
				bean.setClassAddress(rs.getString("class_address"));
				bean.setWeekType(rs.getString("week_type"));
				switch(bean.getWeek()){
					case MsgConstants.SCHEDULE_TYPE_DEFAULT:
						bean.setWeekTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TYPE_DEFAULT));
						break;
					case MsgConstants.SCHEDULE_TYPE_SINGLE:
						bean.setWeekTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TYPE_SINGLE));
						break;
					case MsgConstants.SCHEDULE_TYPE_DOUBLE:
						bean.setWeekTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TYPE_DOUBLE));
						break;
				}
				return bean;
			}
		}
	
	
	 class ClassScheduleDetailMapper implements RowMapper {
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				ClassScheduleDetailBean bean = new ClassScheduleDetailBean();
				bean.setId(rs.getInt("id"));
				bean.setClassScheduleId(rs.getInt("class_schedule_id"));
				bean.setWeek(rs.getInt("week"));
				bean.setScheduleSettingId(rs.getInt("class_schedule_setting_id"));
				bean.setSubjects(rs.getString("subjects"));
				bean.setStartWeek(rs.getString("stat_week"));
				bean.setEndWeek(rs.getString("end_week"));
				bean.setTeacherId(rs.getString("teacher_id"));
				bean.setClassAddress(rs.getString("class_address"));
				bean.setWeekType(rs.getString("week_type"));
				try{
					switch(bean.getWeek()){
						case MsgConstants.SCHEDULE_TYPE_DEFAULT:
							bean.setWeekTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TYPE_DEFAULT));
							break;
						case MsgConstants.SCHEDULE_TYPE_SINGLE:
							bean.setWeekTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TYPE_SINGLE));
							break;
						case MsgConstants.SCHEDULE_TYPE_DOUBLE:
							bean.setWeekTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_PAGE_WEEK_TYPE_DOUBLE));
							break;
					}
				}catch(Exception e){}	
				try{
					bean.setNumber(rs.getInt("number"));
					bean.setScheduleType(rs.getInt("class_schedule_type"));//作息时间类型
					try{
						switch(bean.getScheduleType()){
							case Contants.SCHEDULE_TYPE_MORNING:
								bean.setScheduleTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_PAGE_TIME_MORNING));
								break;
							case Contants.SCHEDULE_TYPE_FORENOON:
								bean.setScheduleTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_PAGE_TIME_FORENOON));
								break;
							case Contants.SCHEDULE_TYPE_AFTERNOON:
								bean.setScheduleTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_PAGE_TIME_AFTERNOON));
								break;
							case Contants.SCHEDULE_TYPE_NIGHT:
								bean.setScheduleTypeLabel(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_PAGE_TIME_NIGHT));
								break;
						}
					}catch(Exception e){}	
					bean.setStartTime(rs.getString("start_time"));
					bean.setEndTime(rs.getString("end_time"));
				}catch(Exception e){
					log.error("get some cols failed : ", e);
				}
				return bean;
			}
		}
	
}
